package web;

import data.Show;
import web.rssReader.DailyTvTorrentRssReader;
import web.rssReader.EztvRssReader;
import web.xmlReader.TvRageXmlReader;

public class PopulateShow 
{

	private TvRageXmlReader tvrage;
	private DailyTvTorrentRssReader daily;
	private EztvRssReader eztv;
	
	public PopulateShow(String showName)
	{
		tvrage = new TvRageXmlReader(showName);
		eztv = new EztvRssReader(showName);
		daily = new DailyTvTorrentRssReader(showName);
	}
	
	public Show getShow()
	{
		Show returnShow;
		
		returnShow = tvrage.getShow();
		Show dailyShow = daily.getShow();
		Show eztvShow = eztv.getShow();
		if(returnShow != null)
		{
			if(dailyShow != null)
			{
				returnShow.merge(dailyShow);
			}
			if(eztvShow != null)
			{
				returnShow.merge(eztvShow);
			}
		}
		return returnShow;
	}
	
	
	public boolean updateShow(Show a)
	{
		/*
		 * Right now TvRageXmlReader and EztvRssReader are assigned the responsibility of
		 * updating shows, but this method should do the work.
		 * 
		 *  Reason being is time complexity's 
		 *  	TvRageXmlReader.updateShow() == O(newSeasons.size()^2 * newE.size())
		 *  	EztvRssReader.updateShow() == O(newSeasons.size()^2 * newE.size())
		 *  
		 *  	Making this method == O(newSeasons.size()^4 * newE.size^2) == ouch
		 *  
		 *  These method's only achieve set's and get's so it would be nice to recognize
		 *  a episode has changed in this method and then do all the set's and get's 
		 *  at the same time, cutting the time complexity in half. 
		 */
		boolean showUpdated = false;
		/*
		Show tvRageShow = tvrage.getShow();
		Show dailyShow = daily.getShow();
		Show eztvShow = eztv.getShow();
		
		*/
		/*
		if(tvRageShow != null && a.merge(tvRageShow) ||
		   dailyShow != null && a.merge(dailyShow) ||
		   eztvShow != null && a.merge(eztvShow))
		{
			showUpdated = true;
		}
		*/
		Show newShow = this.getShow();
		newShow.setSelectedEpisode(1, 1);
		/**
		 * @TODO - fix this hack to always take the merge'd selected episode. 
		 * a
		 */
		int selectedSeason = a.getSelectedSeason().getSeasonNumber();
		int selectedEpisode = a.getSelectedSeason().getSelectedEpisode().getEpisodeNumber();
		if(a.merge(newShow))
		{
			showUpdated = true;
			a.setSelectedEpisode(selectedSeason, selectedEpisode);
		}
		newShow = null;
		return showUpdated;
		
	}
}
